<?php
/*
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 2021-06-23 15:05:51
 * @LastEditors: 偻儸小卒[EdisonLiu_]
 * @LastEditTime: 2021-06-24 14:31:59
 * @Description: 
 */

namespace App\Http\Controllers\AdminApi\Member;

use App\Http\Controllers\Controller;
use App\Http\Models\Admin\ClassifyPermissions;

use App\Http\Models\Admin\Member\Inspection;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InspectionController extends Controller
{
    // 查列表
    public function list(Request $request)
    {

        $limit = $request->get('limit');
        $title = $request->get('title');
        $sort = getSort($request);
        // var_dump($sort);die;
        // 
        $items = Inspection::orderBy($sort[0], $sort[1])->paginate($limit);

        return response()->json(['code' => 20000, 'message' => '角色查询成功', 'data' => $items]);
    }
    // 查一条
    public function getinfo(Request $request)
    {
        $id = $request->get('id');
        // redaySql(new DB);
        $Inspection = Inspection::find($id);
        // showSql(new DB);
        return response()->json(['code' => 20000, 'message' => '督查员查询成功', 'data' => compact('Inspection')]);
    }
    // 创建
    public function create(Request $request)
    {
        $Inspection = new Inspection;
        $Inspection->username = $request->get('username');

        // 判断用户名是否存在
        $is_Inspection = $Inspection->where([['username', '=', $Inspection->username]])->first();
        if (!empty($is_Inspection)) {
            return response()->json(['code' => 10001, 'message' => '该用户名已存在']);
        }
        $Inspection->title = $request->get('title');
        $Inspection->mobile = $request->get('mobile');
        $Inspection->password = $request->get('password');
        $Inspection->username = $request->get('username');
        $Inspection->address = $request->get('address');
        $Inspection->ascription = 0;

        $Inspection->save();

        return response()->json(['code' => 20000, 'message' => '修改督查员成功', 'data' => compact('Inspection')]);
    }

    //修改
    public function update(Request $request)
    {

        $id = $request->get('id');
        if (empty($id)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }
        $Inspection = Inspection::findOrFail($id);
        if (empty($Inspection)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }

        $req_username = $request->get('username');
        // 如果该人员原有的用户名不是当前提交的用户名，并且当前用户名在数据库中已存在，则提示用户重名
        $is_Inspection = $Inspection->where([['username', '=', $req_username]])->count();
        if (($is_Inspection == 1 && $Inspection->username != $req_username) || $is_Inspection > 1) {
            return response()->json(['code' => 10001, 'message' => '该用户名已存在']);
        }

        $Inspection->title = $request->get('title');
        $Inspection->realname = $req_username;
        $Inspection->mobile = $request->get('mobile');
        $Inspection->password = $request->get('password');
        $Inspection->username = $request->get('username');
        $Inspection->address = $request->get('address');
        $Inspection->ascription = 0;

        $Inspection->update();

        // dd($ress);die;
        return response()->json(['code' => 20000, 'message' => '修改督查员成功', 'data' => compact('Inspection')]);
    }
    // 删除
    public function delete(Request $request)
    {
        $id = $request->get('id');
        $Inspection = Inspection::findOrFail($id);
        $Inspection->delete();
        return response()->json(['code' => 20000, 'message' => '删除成功', 'data' => $id]);
    }
}
